package com.example.ceramics.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ceramics.R;
import com.example.ceramics.custom.CustomAddressPicker;
import com.example.ceramics.custom.TextAddressLoader;
import com.example.ceramics.custom.TextAddressParser;
import com.example.ceramics.database.dao.IUserDao;
import com.example.ceramics.database.dao.UserDaoImpl;
import com.example.ceramics.entity.UserEntity;
import com.example.ceramics.util.StringUtils;
import com.github.gzuliyujiang.dialog.DialogColor;
import com.github.gzuliyujiang.dialog.DialogConfig;
import com.github.gzuliyujiang.dialog.DialogStyle;
import com.github.gzuliyujiang.wheelpicker.AddressPicker;
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode;
import com.github.gzuliyujiang.wheelpicker.contract.OnAddressPickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnLinkageSelectedListener;
import com.github.gzuliyujiang.wheelpicker.entity.CityEntity;
import com.github.gzuliyujiang.wheelpicker.entity.CountyEntity;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import com.github.gzuliyujiang.wheelpicker.utility.AddressJsonParser;
import com.ywp.addresspickerlib.AddressPickerView;

public class AddressActivity extends BaseActivity implements OnAddressPickedListener {
    private TextView tvAddress;
    private EditText etName;
    private EditText etPhone;
    private EditText etAddress;
    private String name;
    private String phone;
    private String address;
    private String addressDetail;
    private String addressProvince;
    private String addressCity;
    private String addressCounty;

    private IUserDao userDao;

    @Override
    protected int initLayout() {
        return R.layout.activity_address;
    }

    @Override
    protected void initView() {
        tvAddress = findViewById(R.id.tv_address);
        etName = findViewById(R.id.et_name);
        etPhone = findViewById(R.id.et_phone);
        etAddress = findViewById(R.id.et_address);
    }

    @Override
    protected void initData() {
        userDao = new UserDaoImpl(this);
        UserEntity user = userDao.getUser(getStringFromSp("account"));
        etName.setText(user.getName());
        etPhone.setText(user.getPhone());
        if (!StringUtils.isEmpty(user.getAddressProvince())) {
            address = user.getAddressProvince() + " · " + user.getAddressCity() + " · " + user.getAddressCounty();
            tvAddress.setText(address);
            tvAddress.setTextColor(getResources().getColor(R.color.black));
        }
        etAddress.setText(user.getAddressDetail());

//        SharedPreferences spFile = getSharedPreferences("address", Context.MODE_PRIVATE);
//        name = spFile.getString("name", null);
//        phone = spFile.getString("phone", null);
//        address = spFile.getString("address", null);
//        addressDetail = spFile.getString("addressDetail", null);
//        addressProvince = spFile.getString("addressProvince",null);
//        addressCity = spFile.getString("addressCity",null);
//        addressCounty = spFile.getString("addressCounty",null);
//
//        etName.setText(name);
//        etPhone.setText(phone);
//        tvAddress.setText(address);
//        tvAddress.setTextColor(getResources().getColor(R.color.black));
//        etAddress.setText(addressDetail);
    }

    public void ivReturn(View view) {
        finish();
    }

    public void save(View view) {
        UserEntity user = new UserEntity();
        user.setAccount(getStringFromSp("account"));
        user.setName(etName.getText().toString());
        user.setPhone(etPhone.getText().toString());
        user.setAddressProvince(addressProvince);
        user.setAddressCity(addressCity);
        user.setAddressCounty(addressCounty);
        user.setAddressDetail(etAddress.getText().toString());
        int result = userDao.updateAddress(user);
        if (result == 1) {
            showToast("保存成功！");
        } else {
            showToast("保存失败！");
        }

//        SharedPreferences spFile = getSharedPreferences("address", Context.MODE_PRIVATE);
//        SharedPreferences.Editor editor = spFile.edit();
//        editor.putString("name", etName.getText().toString());
//        editor.putString("phone", etPhone.getText().toString());
//        editor.putString("address", tvAddress.getText().toString());
//        editor.putString("addressDetail", etAddress.getText().toString());
//        editor.putString("addressProvince", addressProvince);
//        editor.putString("addressCity",addressCity);
//        editor.putString("addressCounty",addressCounty);
//        editor.apply();
//        showToast("保存成功！");
    }

    public void onProvinceCityCounty(View view) {
        AddressPicker picker = new AddressPicker(this);
        picker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY);
        picker.setOnAddressPickedListener(this);
        picker.setDefaultValue(addressProvince, addressCity, addressCounty);
        picker.getWheelLayout().setOnLinkageSelectedListener(new OnLinkageSelectedListener() {
            @Override
            public void onLinkageSelected(Object first, Object second, Object third) {
                picker.getTitleView().setText(String.format("%s%s%s",
                        picker.getFirstWheelView().formatItem(first),
                        picker.getSecondWheelView().formatItem(second),
                        picker.getThirdWheelView().formatItem(third)));
            }
        });
        picker.show();
    }

    public void onCustomUi(View view) {
        CustomAddressPicker picker = new CustomAddressPicker(this);
        picker.setOnAddressPickedListener(this);
        picker.show();
    }

    public void onCustomDataByText(View view) {
        AddressPicker picker = new AddressPicker(this);
        picker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY);
        picker.setAddressLoader(new TextAddressLoader(this), new TextAddressParser());
        picker.setOnAddressPickedListener(this);
        picker.getWheelLayout().setOnLinkageSelectedListener(new OnLinkageSelectedListener() {
            @Override
            public void onLinkageSelected(Object first, Object second, Object third) {
                picker.getTitleView().setText(String.format("%s%s%s",
                        picker.getFirstWheelView().formatItem(first),
                        picker.getSecondWheelView().formatItem(second),
                        picker.getThirdWheelView().formatItem(third)));
            }
        });
        picker.show();
    }

    @Override
    public void onAddressPicked(ProvinceEntity province, CityEntity city, CountyEntity county) {
        tvAddress.setText(province.getName() + " · " + city.getName() + " · " + county.getName());
        tvAddress.setTextColor(getResources().getColor(R.color.black));
        addressProvince = province.getName();
        addressCity = city.getName();
        addressCounty = county.getName();
    }
}